/*
 * DA2Protocol.java
 *
 * Created on 11 marzo 2007, 21.22
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bloomingstars.bluepad.comm;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author vega
 */
public class DA2Protocol
{           
    public static String getCommandString(int cmd)
    {
        String result = (String) commands.get(new Integer(cmd));
        return result;
    }
    
    public static int getCommandCode(String cmd)
    {
        int result = CommandCode.NULL_COMMAND;
        Enumeration keys = commands.keys();
        // Set<CommandCode> keys = commands.keySet();
        boolean foundFlag = false;
        
        for (; keys.hasMoreElements() && !foundFlag; )
        {
            Integer cc = (Integer) keys.nextElement();
            String s = (String) commands.get(cc);
                     
            if (s.equals(cmd))
            {
                result = cc.intValue();
                foundFlag = true;
            }
        }
        
        return result;
    }
    
    private DA2Protocol()
    {
    }
    
    private static void buildCommandTable()
    {
        commands.put(new Integer(CommandCode.NULL_COMMAND), "");
        commands.put(new Integer(CommandCode.MOVE_EAST), ":Me#");
        commands.put(new Integer(CommandCode.RELEASE_EAST), ":Qe#");
        commands.put(new Integer(CommandCode.MOVE_SOUTH), ":Ms#");
        commands.put(new Integer(CommandCode.RELEASE_SOUTH), ":Qs#");
        commands.put(new Integer(CommandCode.MOVE_WEST), ":Mw#");
        commands.put(new Integer(CommandCode.RELEASE_WEST), ":Qw#");
        commands.put(new Integer(CommandCode.MOVE_NORTH), ":Mn#");
        commands.put(new Integer(CommandCode.RELEASE_NORTH), ":Qn#");
        commands.put(new Integer(CommandCode.STOP), "");
        commands.put(new Integer(CommandCode.SET_SPEED_1), ":RG#");
        commands.put(new Integer(CommandCode.SET_SPEED_2), ":RC#");
        commands.put(new Integer(CommandCode.SET_SPEED_3), ":RM#");
        commands.put(new Integer(CommandCode.SET_SPEED_4), ":RS#");
        commands.put(new Integer(CommandCode.GET_TELESCOPE_RA), ":GR#");
        commands.put(new Integer(CommandCode.GET_TELESCOPE_DEC), ":GD#");
        commands.put(new Integer(CommandCode.GET_FIRMWARE_NUMBER), ":GVN#");
        commands.put(new Integer(CommandCode.GET_PRODUCT_NAME), ":GVP#");
    }
    
    private static Hashtable commands = new Hashtable();
    
    static
    {
        buildCommandTable();
    }
}
